package com.intelligence.tio.server;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.json.Json;
import org.tio.server.intf.ServerAioListener;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.packages.GroupMsgRespBody;

/**
* @Title: TioServerAioListener.java 
* @Package com.intelligence.tio.server 
* @Description: Tio 服务器监听器
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午3:57:47 
* @version V1.0
 */
public class TioServerAioListener implements ServerAioListener<TioSessionContext, JsonPacket, Object>
{
	private static Logger log = LoggerFactory.getLogger(TioServerAioListener.class);

	/** 
	 * @param channelContext
	 * @param isConnected
	 * @param isReconnect
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public void onAfterConnected(ChannelContext<TioSessionContext, JsonPacket, Object> channelContext, boolean isConnected, boolean isReconnect) throws Exception
	{
//		log.info("onAfterConnected channelContext:{}, isConnected:{}, isReconnect:{}", channelContext, isConnected, isReconnect);

		//连接后，需要把连接会话对象设置给channelContext
		channelContext.setSessionContext(new TioSessionContext());
	}

	/** 
	 * @param channelContext
	 * @param packet
	 * @param isSentSuccess
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public void onAfterSent(ChannelContext<TioSessionContext, JsonPacket, Object> channelContext, JsonPacket packet, boolean isSentSuccess) throws Exception
	{
//		log.info("onAfterSent channelContext:{}, packet:{}, isSentSuccess:{}", channelContext, Json.toJson(packet), isSentSuccess);
	}

	/** 
	 * @param channelContext
	 * @param packet
	 * @param packetSize
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public void onAfterReceived(ChannelContext<TioSessionContext, JsonPacket, Object> channelContext, JsonPacket packet, int packetSize) throws Exception
	{
//		log.info("onAfterReceived channelContext:{}, packet:{}, packetSize:{}", channelContext, Json.toJson(packet), packetSize);
	}

	/** 
	 * @param channelContext
	 * @param throwable
	 * @param remark
	 * @param isRemove
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public void onAfterClose(ChannelContext<TioSessionContext, JsonPacket, Object> channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception
	{
		log.info(channelContext.getSessionContext().getUserName()+"用户掉线啦。。。。");
//		log.info("onAfterClose channelContext:{}, throwable:{}, remark:{}, isRemove:{}", channelContext, throwable, remark, isRemove);
		//通知其它组里面用户，当前用户掉线
		sendMsgToOtherUser(channelContext);

	}
	
	/**
	* @Title: sendMsgToOtherUser 
	* @Description: 通知其它组里面用户，当前用户掉线
	* @param @param channelContext
	* @param @throws UnsupportedEncodingException    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	private void sendMsgToOtherUser(ChannelContext<TioSessionContext, JsonPacket, Object> channelContext)
			throws UnsupportedEncodingException {
		//通知其它组里面用户，当前用户掉线
		TioSessionContext sessionContext = channelContext.getSessionContext();
		GroupMsgRespBody groupMsgRespBody = new GroupMsgRespBody();
		groupMsgRespBody.setText("用户掉线！");
		groupMsgRespBody.setToGroupId(sessionContext.getGroupId());
		groupMsgRespBody.setFromUserId(sessionContext.getUserId());
		groupMsgRespBody.setFromUserName(sessionContext.getUserName());
		groupMsgRespBody.setFromUserPhone(sessionContext.getUserPhone());
		groupMsgRespBody.setCommand(Command.COMMAND_OUTLINE_RESP.getValue());
		groupMsgRespBody.setFromRoleId(sessionContext.getRoleId());
		JsonPacket respPacket = new JsonPacket();
		respPacket.setBody(Json.toJson(groupMsgRespBody).getBytes(JsonPacket.CHARSET));
		Aio.sendToGroup(channelContext.getGroupContext(),sessionContext.getGroupId(), respPacket);
	}
}
